package com.zerous.framework;

import android.app.*;
import android.os.*;
import android.graphics.*;
import android.view.*;
import android.content.pm.*;
import android.widget.*;
import android.content.*;

public abstract class Game extends Activity
{
	//public static boolean DEBUG = false;
	Graphics graphics;
	FileIO file;
	Input input;
	
	Screen screen;
	RenderView renderView;
	
	GameHandler gameHandler;

	String strbuff = "123";
	EditText t;
	
	public abstract Screen getStartScreen()
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		Display d = getWindowManager().getDefaultDisplay();
		
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置成全屏模式
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//强制为横屏
		
		
		Bitmap framebuffer = Bitmap.createBitmap(d.getWidth(), d.getHeight(), Bitmap.Config.ARGB_8888);
		renderView = new RenderView(this, framebuffer);
		graphics = new Graphics(getAssets(), framebuffer);
		file = new FileIO(this);
		input = new Input(renderView);
		gameHandler = new GameHandler(getMainLooper());
		
		screen = getStartScreen();
		
		Zex.Const.width = d.getWidth();
		Zex.Const.height = d.getHeight();
		Zex.Const.px = d.getWidth() / 960f;
		
		
		Zex.graphics = graphics;
		Zex.file = file;
		Zex.input = input;
		
		setContentView(renderView);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onBackPressed()
	{
		if(screen.parent != null) 
			setScreen(screen.parent);
		else
			super.onBackPressed();
	}
	

	@Override
	protected void onResume()
	{
		// TODO: Implement this method
		super.onResume();
		renderView.resume();
		Zex.graphics = getGraphics();
		Zex.file = getFileIO();
		Zex.input = getInput();
	}

	@Override
	protected void onPause()
	{
		// TODO: Implement this method
		super.onPause();
		renderView.pause();
	}
	
	public void setScreen(Screen s)
	{
		screen = s;
	}
	
	public String getStrBuff()
	{
		return strbuff;
	}
	
	public GameHandler getGameHandler()
	{
		return gameHandler;
	}
	
	public Input getInput()
	{
		return input;
	}
	
	public FileIO getFileIO()
	{
		return file;
	}
	
	public Graphics getGraphics()
	{
		return graphics;
	}
	
	public Screen getCurrentScreen()
	{
		return screen;
	}
	
	class GameHandler extends Handler
	{
		GameHandler(Looper l)
		{
			super(l);
		}

		@Override
		public void handleMessage(Message msg)
		{
			if(msg.getData().getString("toast") != null)
			{
				Toast.makeText(Game.this, msg.getData().getString("toast"), Toast.LENGTH_SHORT).show();
			}
			else if(msg.getData().getString("text").equals("getString"))
			{
				AlertDialog.Builder ad = new AlertDialog.Builder(Game.this).setTitle("当然是要你输入啦");
				t = new EditText(Game.this);
				ad.setView(t).setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface i, int w)
					{
						strbuff = t.getEditableText().toString();
					}
				});
				ad.show();
			}
		}
	}
}
